function add(){
	userdialog("添加用户",null);
}
function query(){
	var y1= $('#cc2').combobox('getValue');
	var s1= $('#cc3').combobox('getValue');
	var y="",s="";
	if(y1=="GUEST"){
    	y="1";
    }
	if(y1=="操作员"){
    	y="10";
    }
    if(y1=="超级管理员"){
    	y="100";
    }
    if(s1!="请选择"){
    	s=s1;
    }
    table(path,"", y, s);
}
$(function(){
	 table(path,"",null,null);

	$('#ss').searchbox({
	    searcher:function(value,name){
	    	//releaselisttable(value,"");
	    	table(path,value,null,null);
	    },
	    prompt:'按关键字'
	});
	$('#cc').combobox({
	    onSelect: function(rec){ 
	    	 var s=$("#cc").combobox('getValue');
	    	 if(s=="普通用户(1)"){
	    		 sysusertable("","1"); 
	    	 }
			 if(s=="管理员(2)"){
				 sysusertable("","2");    		 
			 }
			 if(s=="超级用户(3)"){
				 sysusertable("","3"); 
			 }
	 }
	})

})
function pldel() {
	var a="aaa";
	var checkedItems = $("#qq").datagrid('getSelections');
	var names = [];
	$.each(checkedItems, function(index, item){
		if(item.logname=="admin"){
			alert("不能删除系统用户");
		}else{
            names.push(item.userid);
		}
	});
	delok(names,a);
}
function del(id){
	$.ajax({  
        type:'post',      
        url:path+'users/delet.html',  
        data:"id="+id, 
        cache:false,  
        dataType:'json',  
        success:function(data){
        	$('#qq').datagrid('reload');
        },
	    error:function(){   
	    	 
	    }
    }); 
}
function delok(id,a){
	if(a=="admin"){
		alert("不能删除系统用户");
	}else{
		
	
	$("#okuser").dialog({    
	    title: '确认删除',    
	    width: 196,    
	    height: 150,
	    left: 450,
	    top:100,
	    cache: true, 		    
        collapsible: true,
        minimizable: true,
        maximizable: true,
        resizable: true,
        content: '你确定要删除',
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () { 
            	$('#okuser').dialog('close');
            	del(id);    
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#okuser').dialog('close');
            }
        }]
});}
}
function userdialog(s,rowData){
	$("#edituser").dialog({    
	    title: s,    
	    width:500,    
	    height:400,
	    modal:true,
	    cache: true,
        collapsible:true,
        minimizable:true,
        maximizable:true,
        resizable:true,
	    href:"edituser.jsp",
	    onLoad: function () {  
	    	if(rowData!=null){
	            //$("#username").val(rowData.username);
	            $("#logn").val(rowData.logname);
	            $("#password").val(rowData.password);
	            $('#cc').combobox('setValue',rowData.isused);
	            var valu=rowData.power;
	            var s="GUEST";
			    if(valu==10){
			    	s="操作员";
			    }
	            if(valu==100){
			    	s="超级管理员";
			    }
	            $('#cc1').combobox('setValue',s);
	            $("#qq1").val(rowData.username);
	            $("#tel").val(rowData.tel);
	            $("#remark").val(rowData.remark);
	    	}
        } ,
        onClose: function () {
        	  
            },
            buttons: [{
                text: '确认',
                iconCls: 'icon-ok',
                handler: function () {
                	var userid=null;
                	if(rowData!=null){
                	  userid =rowData.userid;
                	}
                	var username = $("#qq1").val();
                	var logname = $("#logn").val();
                	var password = $("#password").val();
                	if(password.length<6){
                		alert("密码必须6位以上");
                        return false;
                	};
                	if(logname==""||logname==null){
                		alert("登录名不能为空");
                	}
                	var isused = $('#cc').combobox('getValue');
                    var s= $('#cc1').combobox('getValue');
                    var valu=1;
        		    if(s=="操作员"){
        		    	valu=10;
        		    }
                    if(s=="超级管理员"){
        		    	valu=100;
        		    }
                    var power = valu;
                  //  var qq =  $("#qq1").val();
                    var tel = $("#tel").val();
                    var remark = $("#remark").val();
                    var user={"userid":userid,"username":username,"logname":logname,"password":password,"isused":isused,"power":power,"qq":111,"tel":tel,"remark":remark};
                	$.ajax({  
                        type:'post',      
                        url:path+'users/updateuser.html',  
                        data:user, 
                        cache:false,  
                        dataType:'json',  
                        success:function(data){
                        	alert(data.errorMsg+"：登录名已存在");
                        },
                	    error:function(){   
                	    	$('#qq').datagrid('reload');
                	    }
                    });  
                	$('#edituser').dialog('close');
       
                }
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#edituser').dialog('close');
                }
            }]
        
	});    
}
function table(s1,cs,y,s){
	var uri="";
	if(y!=null||s!=null){
		uri=s1+'users/queryusers.html?y='+y+'&&s='+s;
	}else{
		uri=s1+'users/getallusers.html?cs='+cs;
	}
	$("#qq").datagrid({
		url:uri,
	    //queryParams:{},  
	    loadMsg:'数据加载中请稍后……',  
	    pagination: true, 
	    height:480,
	    remoteSort:false,
	    fit:true,
	    rownumbers: true,  
	    fitColumns:true,
	    frozenColumns:[[ 
	                    {field:'ck',checkbox:true} 
	                ]], 
	    pageList: [10,20,30,40,50],
	    pageSize: 20,
	    rowStyler:function(index,row){
	    	var id1 =  $("#dqid").val();
			if (row.userid==id1){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
	    onDblClickRow: function(rowIndex, rowData){
	    	/*if(rowData.logname=="admin"){
	    		alert("系统用户不能操作");
	    	}else{
                userdialog("修改用户",rowData);
	    	}*/
		},
	    columns:[[ 
			{field:'qq',title: '用户名',align: 'center',width:65,sortable:true,hidden:true},  
			{field:'logname',title: '登录名',align: 'center',width:65,sortable:true},
			{field:'password',title: '登录密码',align: 'center',width:65,sortable:true,hidden:true},
			{field:'isused',title: '是否可用',align: 'center',width:65,sortable:true},  
			{field:'power',title: '用户角色',align: 'center',
			   formatter:function(value,rowData,rowIndex){
				    var s1="GUEST";
				    if(value==10){
				    	s1="操作员";
				    }
                    if(value==100){
				    	s1="超级管理员";
				    }
			        return s1; 
			   } ,width:65,sortable:true	
			},  
			{field:'username',title: '岗位',align: 'center',width:65,sortable:true},
			{field:'tel',title: '联系电话',align: 'center',width:65,sortable:true},
			{field:'remark',title: '备注',align: 'center',width:65,sortable:true},  
			{field:'id',title: '功能',align: 'center', 
			    formatter:function(value,rowData,rowIndex){
			        return "<a href='#' onclick=delok('" + rowData.userid + "','"+rowData.logname+"')>删除</a>"; 
			   },width:65,sortable:true  
			}
	    ]],
	    toolbar: [{
			iconCls: 'icon-add',
			text:'新增',
			handler: function(){add()}
		},'-',{
			iconCls: 'icon-edit',
			text:'编辑',
			handler: function(){
				var rowData = $('#qq').datagrid('getSelected');
				var selectrows=$("#qq").datagrid('getSelections');
				//alert(selectrows.length)
				if(selectrows.length==0){
					alert("请选择一条数据进行编辑！");
					return false;
				}else if(selectrows.length==1){
					if(rowData.logname=="admin"){
			    		alert("系统用户不能操作");
			    	}else{
		                userdialog("修改用户",rowData);
			    	}
				}else{
					alert("对不起！当前操作只能选择一条数据进行编辑！");
					return false;
				}
			}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除',
			handler: function(){
				pldel();	
			}
		},'-',{
			iconCls: 'icon-reload',
			text:'显示所有',
			handler: function(){table(path,"",null,null);}
		}]
	});
	  var p = $('#qq').datagrid('getPager'); 
	    $(p).pagination({ 
//	        pageSize: 20,//每页显示的记录条数，默认为10 
//	        pageList: [10,20,30,40,50],//可以设置每页记录条数的列表 
	        beforePageText: '第',//页数文本框前显示的汉字 
	        afterPageText: '页    共 {pages} 页', 
	        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录' 
	    }); 
} 